/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import io.renren.common.utils.Result;
import io.renren.core.dto.LearnKnowSourceDTO;
import io.renren.core.dto.LearnKnowTypeDTO;
import io.renren.core.dto.LearnQuestionInfoDTO;
import io.renren.core.dto.LearnQuestionTypeDTO;
import io.renren.core.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * banner
 */
@RestController
@RequestMapping("/api/question")
@Api(tags = "知识库")
public class ApiQuestionController {

    @Autowired
    private LearnQuestionTypeService learnQuestionTypeService;

    @Autowired
    private LearnQuestionInfoService learnQuestionInfoService;

    @GetMapping("type-list")
    @ApiOperation(value = "列表")
    public Result<?> typeList() {
        List<LearnQuestionTypeDTO> typeList = learnQuestionTypeService.list(Maps.newHashMap());
        List<QuestionTypeResult> resultList = Lists.newArrayList();

        Map<String, List<LearnQuestionTypeDTO>> map = Maps.newHashMap();
        for (LearnQuestionTypeDTO learnQuestionTypeDTO : typeList) {
            List<LearnQuestionTypeDTO> l = map.get(learnQuestionTypeDTO.getFirstType());
            if (Objects.isNull(l)) {
                l = Lists.newArrayList();
                map.put(learnQuestionTypeDTO.getFirstType(), l);
            }
            l.add(learnQuestionTypeDTO);
        }

        for (Map.Entry<String, List<LearnQuestionTypeDTO>> entry : map.entrySet()) {
            QuestionTypeResult qtr = QuestionTypeResult.builder()
                    .name(entry.getKey())
                    .typeList(entry.getValue())
                    .build();
            qtr.getTypeList().sort(Comparator.comparing(LearnQuestionTypeDTO::getSort));
            Collections.reverse(qtr.getTypeList());
            resultList.add(qtr);
        }
        return new Result<>().ok(resultList);
    }

    @GetMapping("question-type-by-id")
    @ApiOperation(value = "获取知识库数据")
    public Result<?> questionTypeById(Long id) {
        return new Result<>().ok(learnQuestionTypeService.get(id));
    }

    @GetMapping("day-question-by-type-id")
    @ApiOperation(value = "获取知识库数据")
    public Result<?> dayQuestionByTypeId(Long questionTypeId) {
        List<LearnQuestionInfoDTO> sourceList = learnQuestionInfoService.listByTypeId(questionTypeId);
        int number = 10;
        if (sourceList.size() < 10) {
            return new Result<>().ok(sourceList);
        } else {
            List<LearnQuestionInfoDTO> list = Lists.newArrayList();
            for (int i = 0; i < number; i++) {
                int index = RandomUtil.randomInt(sourceList.size());
                list.add(sourceList.get(i));
                sourceList.remove(index);
            }
            return new Result<>().ok(list);
        }
    }

    @GetMapping("chapter-question-by-type-id")
    @ApiOperation(value = "获取知识库数据")
    public Result<?> chapterQuestionByTypeId(Long questionTypeId) {
        List<LearnQuestionInfoDTO> sourceList = learnQuestionInfoService.listByTypeId(questionTypeId);
        List<ChapterQuestionResult> resultList = Lists.newArrayList();
        sourceList = sourceList.stream().filter(x -> Objects.equals(0, x.getQuestionReal())).collect(Collectors.toList());
        List<String> chapterList = sourceList.stream().map(LearnQuestionInfoDTO::getChapter).distinct().collect(Collectors.toList());

        for (String s : chapterList) {
            List<LearnQuestionInfoDTO> l = sourceList.stream().filter(x -> Objects.equals(s, x.getChapter())).collect(Collectors.toList());
            resultList.add(ChapterQuestionResult.builder()
                    .name(s)
                    .sort(l.get(0).getSort())
                    .infoList(l)
                    .build());
        }

        return new Result<>().ok(resultList);
    }

    @GetMapping("real-question-by-type-id")
    @ApiOperation(value = "获取知识库数据")
    public Result<?> realQuestionByTypeId(Long questionTypeId) {
        List<LearnQuestionInfoDTO> sourceList = learnQuestionInfoService.listByTypeId(questionTypeId);
        List<ChapterQuestionResult> resultList = Lists.newArrayList();
        sourceList = sourceList.stream().filter(x -> Objects.equals(1, x.getQuestionReal())).collect(Collectors.toList());
        List<String> chapterList = sourceList.stream().map(LearnQuestionInfoDTO::getChapter).distinct().collect(Collectors.toList());

        for (String s : chapterList) {
            List<LearnQuestionInfoDTO> l = sourceList.stream().filter(x -> Objects.equals(s, x.getChapter())).collect(Collectors.toList());
            resultList.add(ChapterQuestionResult.builder()
                    .name(s)
                    .sort(l.get(0).getSort())
                    .infoList(l)
                    .build());
        }

        return new Result<>().ok(resultList);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(RandomUtil.randomInt(1));
        }
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class QuestionTypeResult {
        private String name;
        private List<LearnQuestionTypeDTO> typeList;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class ChapterQuestionResult {
        private String name;
        private Integer sort;
        private List<LearnQuestionInfoDTO> infoList;
    }

}